import miniApp, {
    analyticsBase,
    authorizeParams,
    gameRecorderStartParams,
    getUserInfoParams,
    loadParams,
    loginParams, ModalParams, SHARE_VIDEO_SORT_TYPE, ShareVideoRankDetail,
    toastParams,
    UserInfo
} from "../miniApp";
import O1HD from "../../../gameEngine";
import { DEEP_NEXT_SPLIT } from "../../../configs/logConfig";
import { dataField } from "../dataField";

//
// window['lookOverF'] = null;
// export function androidCallVideoOver(){
//     window['lookOverF']  && window['lookOverF'] ();
// }

export default class NativeApi extends miniApp {
    navigateToMiniGame(appId: string, successCallback?: () => void, failCallback?: () => void): void {
        console.error('#23102559 未实现跳转');
    }

    drawHead(headUrl: string): Promise<cc.SpriteFrame> {
        return Promise.resolve(undefined);
    }
    async fullScreenVideo(): Promise<void> {
        //
        switch (cc.sys.os) {
            case cc.sys.OS_ANDROID: {
                return Promise.resolve();
            }
            case cc.sys.OS_IOS: {
                console.log('cocos SDK 调用苹果 全屏视频');
                //苹果代码并没有使用此ID
                return new Promise(resolve => {
                    //@ts-ignore
                    jsb.reflection.callStaticMethod('RootViewController', 'loadFullScreenVideo');
                    resolve();
                });
            }
            default: {
                return Promise.resolve();
            }
        }
    }
    async commitRank(rankId: dataField.RANK_ID, score: number): Promise<boolean> {
        return Promise.resolve(false);
    }

    async showRank(rankId: dataField.RANK_ID): Promise<boolean> {
        return Promise.resolve(false);
    }

    LocalInBlockCityList(): boolean {
        return false;
    }

    vibrate(type): void {

    }

    showBannerAD(node: cc.Node, successCallback?: () => void, errorCallback?: (error: any) => void, onCloseCallBack?: () => void, setBannerPosition?: ({ screenWidth, screenHeight }: { screenWidth: any; screenHeight: any; }) => cc.Vec2): void {
        throw new Error("Method not implemented.");
    }

    hideBanner(): void {
        throw new Error("Method not implemented.");
    }

    showInterstitialAd(onAdClose: () => void, onFailed: () => void): void {
        throw new Error("Method not implemented.");
    }

    showNativeAd(successCallback: (a: any[]) => void, errorCallback: (error: any) => void, completeCallback?: () => void): void {
        throw new Error("Method not implemented.");
    }

    reportAdClickNativeAd(): void {
        throw new Error("Method not implemented.");
    }

    createMoreGameBtn(): void {
        throw new Error("Method not implemented.");
    }

    addShortcut(): Promise<boolean> {
        throw new Error("Method not implemented.");
    }

    operateGameLive(): void {
        throw new Error("Method not implemented.");
    }

    getShareVideoRank(sortType: SHARE_VIDEO_SORT_TYPE, successCb: (data: ShareVideoRankDetail[]) => void): void {
        throw new Error("Method not implemented.");
    }

    drawUrlImage(url: string, overCb: (spf: cc.SpriteFrame) => void): void {
        throw new Error("Method not implemented.");
    }

    showVideo(videoUrl: string): void {
        throw new Error("Method not implemented.");
    }

    subscribeMessage(successCallback?: () => void, failCallback?: () => void, completeCallback?: () => void): void {
        throw new Error("Method not implemented.");
    }

    showAddFavoriteGuide(successCallback?: () => void, failCallback?: () => void, completeCallback?: () => void): void {
        throw new Error("Method not implemented.");
    }

    StartRecordSound(): void {
        throw new Error("Method not implemented.");
    }

    StopRecordSound(successCallback?: () => void): void {
        throw new Error("Method not implemented.");
    }

    PlayLastRecordSound(scale?: number, successCb?: () => void, errorCb?: () => void): void {
        throw new Error("Method not implemented.");
    }

    protected m_appId: string = '205615';
    protected m_secret: string = '';
    protected m_adUnitId: string = '945669906';

    static getRandNameAndUrl(): UserInfo {
        let user: UserInfo = {
            nickName: `零一侠`,//侠客${data_control.getRandId(6)}
            avatarUrl: null,
            language: 'ch',
            gender: 0,
            country: '中国',        //用户所在国家
            province: '',        //用户所在省份
            city: '',            //用户所在城市
        };

        return user;
    };

    validAppId(): void {
    }

    getUserInfo(params: getUserInfoParams): void {
        params.success && params.success({ userInfo: NativeApi.getRandNameAndUrl() });
    }

    showToast(params: toastParams): void {
        // O1HD.getManagers().uiMgr.showMessageBox(params.title);
    }

    showLoading(params: loadParams): void {
        console.log('h5 显示进度', params);
    }

    hideLoading(): void {
        console.log('h5 隐藏进度');
    }

    preCreateUnits(): void {
        // console.log(DEEP_NEXT_SPLIT, 'h5 预先创建');
    }

    loadAndShowVideoAd(successCallback: () => void, errorCallback: (error: any) => void, completeCallback?: () => void): void {
        //
        switch (cc.sys.os) {
            case cc.sys.OS_ANDROID: {
                // 调用 hello 方法
                // completeCallback && completeCallback();
                // window['lookOverF'] = completeCallback;
                jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "loadRewardingAd", "(Ljava/lang/String;)V", "native.androidCallVideoOver()");
                // cc.game.pause();
                // O1HD.getManagers().uiMgr.showLoading('正在加载视频');
                let dr = setInterval(() => {
                    //@ts-ignore
                    let okNumber = jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getAdWatchFlag', "(Ljava/lang/String;)I", "test");
                    console.log('cocos SDK 广告结果', okNumber);
                    if (okNumber == 1) {
                        clearInterval(dr);
                        // O1HD.getManagers().uiMgr.hideLoading();
                        // cc.game.resume();
                        //
                        successCallback && successCallback();
                        completeCallback && completeCallback();
                    }
                }, 100);
                break;
            }
            case cc.sys.OS_IOS: {
                console.log('cocos SDK 调用苹果激励视频', this.m_adUnitId);
                //苹果代码并没有使用此ID
                jsb.reflection.callStaticMethod('RootViewController', 'loadRewardVideo:', this.m_adUnitId);
                // cc.game.pause();
                // O1HD.getManagers().uiMgr.showLoading('正在加载视频');
                let dr = setInterval(() => {
                    //@ts-ignore
                    let okNumber = jsb.reflection.callStaticMethod('RootViewController', 'getRewardLookOver');
                    console.log('cocos SDK 广告结果', okNumber);
                    if (okNumber) {
                        clearInterval(dr);
                        // O1HD.getManagers().uiMgr.hideLoading();
                        // cc.game.resume();
                        //
                        successCallback && successCallback();
                        completeCallback && completeCallback();
                    }
                }, 100);
                break;
            }
            default: {
                successCallback && successCallback();
                completeCallback && completeCallback();
                break;
            }
        }
    }

    authorize(params: authorizeParams): void {
        params.success && params.success();
    }

    login(params: loginParams): void {
        params.success && params.success({ code: null });
    }


    gameRecordStart(params: gameRecorderStartParams, onStart?: (res) => void): void {
        console.log('h5 录屏start');
        onStart && onStart(null);
    }

    gameRecordStop(): boolean {
        console.log('h5 录屏stop');
        return true;
    }

    shareGameRecordVideo(successCb?: () => void, failCb?: () => void): void {
        console.log('h5 分享录屏');
    }

    isProduction(): boolean {
        if (cc.sys.isMobile) {
            return true;
        }
        return false;
    }

    isHaveShareVideo(): boolean {
        return false;
    }

    reportAnalytics(EventKey: string, data: any): void {
        // console.log('native 发送埋', analytics.getKey(), analytics.getObj());
        switch (cc.sys.os) {
            case cc.sys.OS_ANDROID: {
                // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "eventListener", "(Ljava/lang/String;Ljava/lang/String;)V", analytics.getKey(), analytics.getObj().src);
                // jsb.reflection.callStaticMethod('RootViewController', 'reportAnalytics:valueString:', analytics.getKey(), analytics.getObj().src);
                break;
            }
            case cc.sys.OS_IOS: {
                // console.log('cocos SDK 调用苹果', analytics.getKey(), analytics.getObj().src);
                // jsb.reflection.callStaticMethod('RootViewController', 'reportAnalytics:valueString:', analytics.getKey(), analytics.getObj().src);
                break;
            }
            default: {
                break;
            }
        }
        return;
    }

    clearMemory(): void {

    }

    shareAppMessage(args: any): void {
        // O1HD.getManagers().uiMgr.showMessageBox('分享请从微信小游戏进入');
    }

    showModal(params: ModalParams): void {
    }
}


















